package com.mpsc.current.unit;


import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.processing.Operations;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.GeneralEnvelope;
import org.geotools.referencing.ReferencingFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.util.factory.Hints;
import org.opengis.coverage.Coverage;
import org.opengis.coverage.grid.GridEnvelope;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import javax.media.jai.ImageLayout;
import java.awt.color.ColorSpace;
import java.awt.image.ColorModel;
import java.awt.image.SampleModel;
import java.io.File;
import java.io.IOException;

public class GeoToolsUtils {

    private static final String TIF_PATH = "D:\\Gis开发\\数据\\影像数据\\tiff\\china.tif";

    /**
     * geoTool 读取 .tif文件
     */
    public void readTiffFile() throws IOException {
        /**
         * 使用GeoTiffReader
         */
        Hints tiffHints = new Hints();
        tiffHints.add(new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE));
        tiffHints.add(new Hints(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, DefaultGeographicCRS.WGS84));
        File file = new File(TIF_PATH);
        GeoTiffReader tiffReader = new GeoTiffReader(file, tiffHints);
        // GeoTiffReader tiffReader = new GeoTiffReader(TIF_PATH);
        //获取到其bbox
        GeneralEnvelope envelope = tiffReader.getOriginalEnvelope();

        //获取到投影信息
        CoordinateReferenceSystem crs = tiffReader.getCoordinateReferenceSystem();
        //获取tiff的gridrange(网格范围，按像素说就是像素数量，也就是是tiff的rectangle)
        GridEnvelope gridEnvelope = tiffReader.getOriginalGridRange();
        //获取其图像相关信息
        ImageLayout imageLayout = tiffReader.getImageLayout();
        //获取colormodel
        ColorModel colorModel = imageLayout.getColorModel(null);
        //查看其通道数量
        int num = colorModel.getNumComponents();
        //是否支持透明
        boolean isAlpha = colorModel.hasAlpha();
        //获取颜色空间
        ColorSpace clorspace = colorModel.getColorSpace();
        //获取samplemodel
        SampleModel sampleModel = imageLayout.getSampleModel(null);
        //数据类型
        int datatype = sampleModel.getDataType();
    }

    /**
     * 转换 .tif文件的坐标系
     */
    public void transformCoordinates(String tifPath) throws FactoryException, IOException {
        //resample参数修改
        File file = new File(tifPath);
        if (file.exists()) {
            //Reader br = new Reader();
            //GridCoverage2D old2D = br.getGridCoverage2D(file);
            Hints tiffHints = new Hints();
            Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
            tiffHints.add(hints);
            tiffHints.add(new Hints(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, DefaultGeographicCRS.WGS84));

            GeoTiffReader tiffReader = new GeoTiffReader(file, tiffHints);
            GridCoverage2D old2D = tiffReader.read(null);
            final CoordinateReferenceSystem sourceCRS = old2D.getCoordinateReferenceSystem();
            System.out.println(String.format("源坐标系为: %s", sourceCRS.getName()));

            CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
            CoordinateReferenceSystem crs = factory.createCoordinateReferenceSystem("EPSG:4326");
            Coverage resample = Operations.DEFAULT.resample(old2D, crs);
            System.err.println(String.format("目标坐标系为: %s", resample.getCoordinateReferenceSystem().getName()));

            System.out.println(""+tiffReader.read(null).getCoordinateReferenceSystem().getName());


            //final CoordinateReferenceSystem WGS = CRS.decode("EPSG:3857");
            //GridCoverage2D new2D = (GridCoverage2D) Operations.DEFAULT.resample(old2D, WGS);
            // System.err.println(String.format("目标坐标系为: %s", new2D.getCoordinateReferenceSystem().getName()))；

//            GridCoverageFactory factory1 = new GridCoverageFactory();
//            GridCoverage2D outputCoverage = factory1.create("subtractTiff",, resample);
//            GeoTiffWriter writer = new GeoTiffWriter(new File(tempTiff));
//            writer.write(outputCoverage, null);
//            writer.dispose();
        }
    }

}
